<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/lion-platform" %>
<html>
<head>
	<meta charset="UTF-8" />
	<link rel="stylesheet" href="${webcontext}/res/style/S.css" />
	<script type="text/javascript" src="${webcontext }/res/js/jquery.min.js"></script>
	<script type="text/javascript" src="${webcontext }/res/js/S.js"></script>	
	<script type="text/javascript">
		$(function(){
			var tbl = new S.Table({
				el:'#br_projects',
				pager:true,
				scroll:true,
				pageSize:30,
				multiselect:false,
				menus:{
					'修改':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data ==null){return;}
						editProject(data);
					},
					'删除':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						
						if(data ==null){return;}
						deleteProject([data]);
					}
				},
				dbclick:function(data){
					editProject(data);
				},
				click:function(d){
					userTbl.load('${webcontext }/${pathvar.admin}/br/project/users?projectId='+d.id);
					modules_tree.load('${webcontext }/${pathvar.admin}/br/project/modules/list?projectId='+d.id);
				}
			});	
			
			//tree
			var modules_tree = new S.Tree({
				el:'#br_project_modules',
				ds:'${webcontext}/${pathvar.admin}/blank',
				render:function(node){
					return '<a class="icons brick"></a> '+node.name;
				}
			});
			
			var userTbl = new S.Table({
				el:'#br_projects_users',
				pager:true,
				scroll:true,
				pageSize:30
			});
			
			$('#btn_project_add').click(function(){
				
				var dlg = new S.Dialog({
					title:'增加项目',
					width:400,
					height:200,
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/project/edit',
					buttons:{
						'保存':function(){
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/project/save',
								data:dlg.form.param(),
								success:function(){
									tbl.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();
			});
			
			
			function editProject(proj){
				var dlg = new S.Dialog({
					title:'编辑项目',
					width:600,
					height:500,
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/project/edit?projectId='+proj.id,
					buttons:{
						'保存':function(){
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/project/update',
								data:dlg.form.param(),
								success:function(){
									tbl.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();
			}
			
			function deleteProject(projs){
				S.confirm('警告','确认要删除选择的项目?',function(){
					var ids=  [];
					S.each(projs,function(i,item){
						ids.push(item.id);
					});
					S.request({
						url:'${webcontext}/${pathvar.admin}/br/project/delete',
						data:{ids:ids.join(',')},
						success:function(){
							new S.Info({text:'删除成功',type:'success'}).show();
							tbl.reload();
						}
					});
				},'warn');
			}
			
			$('#btn_project_edit').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length!=1){new S.Info({text:'请选择1个项目',type:'warn'}).show();return;}
				
				editProject(items[0]);
			});
			$('#btn_project_delete').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length==0){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				deleteProject(items);
			});
			//models
			$('#project_modules_add').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length!=1){new S.Info({text:'请选择1个项目',type:'warn'}).show();return;}
				var prow = modules_tree.getSelectedItems();
				var pid = -1;
				if(prow.length>0){
					pid = prow[0].data.id;
				}
				var dlg = new S.Dialog({
					title:'增加模块',
					url:'${webcontext}/${pathvar.admin}/br/project/modules/edit?projectId='+items[0].id+'&pid='+pid,
					width:400,
					height:200,
					buttons:{
						'确认':function(){
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/project/modules/save',
								data:S.fixConfig({pid:pid,projectId:items[0].id},this.form.param()),
								success:function(){
									new S.Info({text:'保存成功',type:'success'}).show();
									modules_tree.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();			
				
			});	
			$('#project_modules_edit').click(function(){
				
				var prow = modules_tree.getSelectedItems();
				if(prow.length==0){
					new S.Info({text:'请选择模块',type:'warn'}).show();return;
				}
				
				var dlg = new S.Dialog({
					title:'增加模块',
					url:'${webcontext}/${pathvar.admin}/br/project/modules/edit?id='+prow[0].data.id,
					width:400,
					height:200,
					buttons:{
						'确认':function(){
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/project/modules/update',
								data:this.form.param(),
								success:function(){
									new S.Info({text:'修改成功',type:'success'}).show();
									modules_tree.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();		
				
				
			});	
			$('#project_modules_delete').click(function(){
				
				var prow = modules_tree.getSelectedItems();
				if(prow.length==0){
					new S.Info({text:'请选择模块',type:'warn'}).show();return;
				}
				S.confirm('确认','确定要删除选中的模块?',function(){
					S.request({
						url:'${webcontext}/${pathvar.admin}/br/project/modules/delete',
						data:{id:prow[0].data.id},
						success:function(){
							new S.Info({text:'删除成功',type:'success'}).show();
							modules_tree.reload();
						}
					});
				},'warn');
			});	
			
			//users
			$('#btn_project_user_add').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length==0){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var dlg = new S.Dialog({
					title:'增加项目成员',
					url:'${webcontext}/${pathvar.admin}/user/selectuser',
					width:700,
					height:500,
					modal:true,
					buttons:{
						'添加':function(){
							var rows =  selectUserTable.getCheckedRow();
							if(!rows || rows.length<1){
								new S.Info({text:'请选择用户',type:'warn'}).show();return;
							}
							
							var ids = [];
							S.each(rows,function(i,d){
								ids.push(d.id);
							});
							
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/project/users/add',
								data:{projectId:items[0].id,ids:ids.join(',')},
								success:function(){
									new S.Info({text:'已添加',type:'success'}).show();
									userTbl.reload();
								}
							});							
						},
						'关闭':function(){
							dlg.remove();
						}
					}
				});
				dlg.show();
			});
			
			$('#btn_project_user_delete').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length==0){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var rows =  userTbl.getCheckedRow();
				if(!rows || rows.length<1){
					new S.Info({text:'请选择用户',type:'warn'}).show();return;
				}
				var ids = [];
				S.each(rows,function(i,d){
					ids.push(d.id);
				});
				S.confirm('确认','确认要从项目中删除选中的成员?',function(){
					S.request({
						url:'${webcontext}/${pathvar.admin}/br/project/users/delete',
						data:{projectId:items[0].id,ids:ids.join(',')},
						success:function(){
							new S.Info({text:'已删除',type:'success'}).show();
							userTbl.reload();
						}
					});
				},'warn');
			});
		});	
	</script>
</head>
<body>
	<div class="filled-box">
		<div class="sidebar" style="width: 600px;">
			<div class="toolbar">
				<span class="btn" ><i class="icons application"></i> 项目列表</span>
				<p:priv code="BR.PROJ">
					<span class="btn" id="btn_project_add"><i class="icons application_add"></i> 增加</span>
					<span class="btn" id="btn_project_edit"><i class="icons application_edit"></i> 修改</span>
					<span class="btn" id="btn_project_delete"><i class="icons application_delete"></i> 删除</span>
				</p:priv>
			</div>
			<div class="content">
				<table id="br_projects" ds="${webcontext}/${pathvar.admin}/br/project/list" height="45%">
					<tr>
						<td width="30" seq></td>
						<td width="30" checkall></td>
						<td resize="true">名称</td>
						<td resize="true" width="120">负责人</td>
						<td width="120">创建时间</td>
					</tr>
					<tr>
						<td>@{seq}</td>
						<td><input type="checkbox" name="id"  value="@{id}" /></td>
						<td>@{name}</td>
						<td>@{ownerName}</td>
						<td>@{createTime}</td>
					</tr>
				</table>
			</div>
			<div class="toolbar">
				<span class="btn" style="margin-left: 10px;"><i class="icons group"></i> 项目成员</span>
				<p:priv code="BR.PROJ.Members">
				<span class="btn"  id="btn_project_user_add"><i class="icons user_add"></i> 增加</span>
				<span class="btn" id="btn_project_user_delete"><i class="icons user_delete"></i> 删除</span>
				</p:priv>
			</div>
			<div class="content">
				<table id="br_projects_users" ds="${webcontext}/${pathvar.admin}/blank" height="40%">
					<tr>
						<td width="50" seq></td>
						<td width="50" checkall></td>
						<td width="100">姓名</td>
						<td width="100">部门</td>
						<td>邮箱</td>
					</tr>
					<tr>
						<td>@{seq}</td>
						<td><input type="checkbox" name="id" value="@{id }" /></td>
						<td>@{realName}</td>
						<td>@{orgName}</td>
						<td>@{email}</td>
					</tr>
				</table>
			</div>	
		</div>
		<div class="rightside" style="margin-left: 600px;">
			<div class="toolbar">
				<span class="btn" style="margin-left: 10px;"><i class="icons plugin"></i> 项目模块</span>
				<p:priv code="BR.PROJ.Model">
				<span class="btn" id="project_modules_add"><i class="icons plugin_add"></i> 增加</span>
				<span class="btn" id="project_modules_edit"><i class="icons plugin_edit"></i> 修改</span>
				<span class="btn" id="project_modules_delete"><i class="icons plugin_delete"></i> 删除</span>
				</p:priv>
			</div>
			<div class="content" id="br_project_modules" style="max-width: 400px;">
				<div><i class="icons computer"></i> 项目模块</div>
			</div>
		</div>
	</div>	
</body>
</html>